﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.DataAccess;
using WarehouseManagement.Shared;

namespace WarehouseManagement.BusinessLogic.BC
{
    public class ReceiptBC : BaseBC
    {
        public string GenerateReceiptNumber()
        {
            decimal no = 1;
            var today = DateTime.Now;
            var prefix = "PT";

            var list = Context.Receipts.OrderByDescending(e => e.CreatedDate).ToList();
            if (list.Count() > 0)
            {
                var first = list.FirstOrDefault();
                if (first != null)
                {
                    var lastNo = decimal.Parse(first.ReceiptNumber.Substring(10, 6));
                    if (first.CreatedDate.Month == today.Month && first.CreatedDate.Year == today.Year)
                        no = lastNo + 1;

                }
            }

            var newNumber = NumberGenerator.GenerateNumber(prefix, today.Month, today.Year, no);
            return newNumber;

        }

        public void AddReceipt(ReceiptDTO dto)
        {
            var entity = ConvertReceipt(dto);
            Context.Receipts.InsertOnSubmit(entity);
            SubmitChanges();
        }

        public void DeleteReceipt(Guid id)
        {
            var entity = GetReceiptEntity(id);
            if (entity != null)
            {
                Context.Receipts.DeleteOnSubmit(entity);
                SubmitChanges();
            }
        }

        public ReceiptDTO GetReceipt(Guid id)
        {
            var entity = GetReceiptEntity(id);
            ReceiptDTO dto = null;
            if (entity != null)
            {
                dto = ConvertReceipt(entity);
            }
            return dto;
        }

        public IEnumerable<ReceiptDTO> GetReceipts()
        {
            var list = Context.Receipts.Select(e => ConvertReceipt(e)).ToList();
            return list;
        }

        public IEnumerable<ReceiptDTO> GetReceipts(DateTime fromDate, DateTime toDate)
        {
            var list = Context.Receipts
                .Where(e => e.CreatedDate.Date >= fromDate.Date && e.CreatedDate.Date <= toDate.Date)
                .Select(e => ConvertReceipt(e)).ToList();
            return list;
        }
        private Receipt GetReceiptEntity(Guid id)
        {
            return Context.Receipts.SingleOrDefault(e => e.Id == id);
        }
        private ReceiptDTO ConvertReceipt(Receipt entity)
        {
            return new ReceiptDTO()
            {
                Debt = entity.TotalDebt,
                DocumentDate = entity.DocumentDate,
                DocumentNumber = entity.DocumentNumber,
                Id = entity.Id,
                Paid = entity.Value,
                PartnerId = entity.PartnerId,
                Reason = entity.Reason,
                ReceiptDate = entity.Date,
                ReceiptNumber = entity.ReceiptNumber,
                RemainDebt = entity.RemainDebt,
                PartnerName = entity.PartnerName,
                CreatedDate = entity.CreatedDate,
                CreatedBy = entity.CreatedBy

            };

        }

        private Receipt ConvertReceipt(ReceiptDTO dto)
        {
            return new Receipt()
            {
                TotalDebt = dto.Debt,
                DocumentDate = dto.DocumentDate,
                DocumentNumber = dto.DocumentNumber,
                Id = dto.Id,
                Value = dto.Paid,
                PartnerId = dto.PartnerId,
                Reason = dto.Reason,
                Date = dto.ReceiptDate,
                ReceiptNumber = dto.ReceiptNumber,
                RemainDebt = dto.RemainDebt,
                PartnerName = dto.PartnerName,
                CreatedDate = dto.CreatedDate,
                CreatedBy = dto.CreatedBy
            };

        }
    }
}
